package top.kaoshanji.p1ch02c;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/1/2 20:15
 */
public class MethodHandleExample06 {

    private static final Logger logger = LogManager.getLogger(MethodHandleExample06.class);

    public static void main(String[] args) throws Throwable {
        logger.info("...代码清单 p1ch02c-12：MethodHandle的多次参数绑定的示例...");

        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle mh = lookup.findVirtual(String.class, "indexOf",
                MethodType.methodType(int.class, String.class, int.class));

        // 调用的indexOf方法，返回值是 int，第一个参数是 String，第二个参数是 int

        // mh = mh.bindTo("Hello").bindTo("1"); // 更换一下主题区别就较为明显
        mh = mh.bindTo("Hello").bindTo("l");
        System.out.println(mh.invoke(2));

        logger.info("...值是Hello，调用String#indexOf方法：{}...", "Hello".indexOf("l", 2));

    }

}
